package com.finger.educenter.controller;


import com.finger.commonutils.JwtUtils;
import com.finger.commonutils.R;
import com.finger.commonutils.UcenterMemberOrder;
import com.finger.educenter.entity.UcenterMember;
import com.finger.educenter.entity.vo.LoginVO;
import com.finger.educenter.entity.vo.RegisterVO;
import com.finger.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author finger
 * @since 2022-01-14
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @PostMapping("/login")
    public R loginMember(@RequestBody LoginVO member){

        String token = ucenterMemberService.doLogin(member);
        return R.ok().data("token",token);
    }

    @PostMapping("/registerMember")
    public R registerMember(@RequestBody RegisterVO registerVO){

       ucenterMemberService.register(registerVO);
       return R.ok();
    }

    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        UcenterMember member = ucenterMemberService.getById(memberId);

        return R.ok().data("item",member);
    }
    @GetMapping("/getMemberInfoComment")
    public R getMemberInfoComment(@RequestParam("id") String id){

        UcenterMember member = ucenterMemberService.getById(id);

        return R.ok().data("nickname",member.getNickname()).data("memberId",id).data("avatar",member.getAvatar());
    }

    @GetMapping("/getMemberInfoOrder")
    public UcenterMemberOrder getMemberInfo(@RequestParam("id") String id){
        UcenterMember member = ucenterMemberService.getById(id);

        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();

        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    @GetMapping("/getRegisterCount/{day}")
    public Integer getLoginCount(@PathVariable String day){
        return ucenterMemberService.getRegisterCount(day);
    }

}

